package com.oocl.crm_system;

public class DoublyList<T> {
    public  Node<T> head;
    public  Node<T> tail;
    public  int nodeNum;
    
    DoublyList(){
    	head = null;
    	tail = null;
    }
    
    /*print all the element in linkedList,in this place, we will use Object.toString function print the list */
    public void printList() {
    	Node<T> h = head;
        while(h != null) {
            h.value.toString();
            h = h.next;
        }
    }
    
    /*insert element to linkedList by index*/
    public void insertWithIndex(int index, T value) {
        Node<T> newNode = new Node<T>(value);
        if(index == 0) {
            newNode.next = head;
            newNode.pre = null;
            head = newNode;
            nodeNum++;
            return;
        }
        Node<T> temp = head;
        Node<T> t = temp;
        for(int i = 0 ; i < index ; i ++) {
            t = temp;
            temp = temp.next;
        }
        if(index == nodeNum) {
            t.next = newNode;
            newNode.pre = t;
            newNode.next = null;
            tail = newNode;
            nodeNum++;
            return;
        }
        newNode.next = temp;
        newNode.pre = t;
        temp.pre = newNode;
        t.next = newNode;
        nodeNum++;
    }

    //insert in tail
    public void insertTail(T value){
		if (this.nodeNum == 0) {
			head = new Node<T>(value);
			tail = head;
			nodeNum = 1;
		} else {
			Node<T> tmp = new Node<T>(value);
			tmp.pre = tail;
			tail.next = tmp;
			tail = tmp;
			nodeNum++;
		}
    }	
    
    //insert in head
    public void insertHead(T value){
		if (this.nodeNum == 0) {
			head = new Node<T>(value);
			tail = head;
			nodeNum = 1;
		} else {
			Node<T> tmp = new Node<T>(value);
			tmp.next = head;
			head.pre = tmp;
			head = tmp;
			nodeNum++;
		}
    }
    
    /*find Node by the LinkedList*/
    public Node<T> findNodeByIndex(int index){
        Node<T> temp = head;
        for(int i = 0 ; i < index ; i ++) {
            temp = temp.next;
        }
        return temp;
    }
    
    /* find node by node */
	public Node<T> findNode(T data) {
		Node<T> tmp = head;
		if (head != null) {
			while (tmp != null) {
				if (data.equals(tmp.value)) {
					return tmp;
				}
				tmp = tmp.next;
			}
		}
		return null;
	}    

/* delete node by node */
	public void delNode(T data) {
		Node<T> tmp = findNode(data);
		if (tmp != null) {
			if (tmp.pre == null) {
				head = tmp.next;
				head.pre = null;
			} else if (tmp.next == null) {
				tail = tail.pre;
				tail.next = null;
			} else {
				tmp.pre.next = tmp.next;
				tmp.next.pre = tmp.pre;
			}

			nodeNum--;
		}
	}
	
    /*delete element by index*/
    public void delete(int index) {
        if(index == 0){						//it's case that delete first Node<T>
            head = head.next;
            head.pre = null;
            nodeNum--;
            return;
        }
        Node<T> temp = head;
        Node<T> t = temp;
        for(int i = 0 ; i < index ; i++) {
            t = temp;
            temp = temp.next;
        }
        if(index == nodeNum - 1) {          //it's case that delete last Node<T>
            t.next = null;
            tail = t;
            nodeNum--;
            return;
        }
        t.next = temp.next;
        temp.next.pre = t;
        nodeNum--;
    }	
    /*test*/
    public static void main(String[] args) {
//    	DoublyList<Integer> list = new DoublyList<Integer>();
//    	list.insertWithIndex(0, 2);
//    	list.insertWithIndex(0, 6);
//    	list.insertWithIndex(1, 8);
//    	list.insertWithIndex(3, 10);
//    	
//    	list.insertHead(11);
//    	list.insertHead(12);
//    	list.insertHead(13);
//    	list.insertHead(15);
//    	list.insertTail(0);
//    	list.insertTail(1);
//    	list.insertTail(2);
//    	System.out.println("kdlsafjaslkdklasjfk");
//    	System.out.println(list.findNode(2).value);
//
//    	list.printList();
    	
    	DoublyList<Customer> list = new DoublyList<Customer>();
    	Customer person = new Customer();
    	person.setId(1);
    	person.setName("jordan");
    	Customer person2 = new Customer();
    	person2.setId(2);   
    	person2.setName("joel");
    	Customer person5 = new Customer();
    	person5.setId(5);   
    	person5.setName("nick");
    	
    	list.insertTail(person);
    	list.insertTail(person2);
    	list.insertTail(person5);
    	
    	list.printList();
    	
    }
}